package com.waji.http;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.waji.local.SharedPrefer;
import com.waji.utils.StreamUtils;
 
 

public class Login {
	public static String studentlogin(String username, String password, Context context)
			throws Exception {
		String path = "http://10.129.55.180:8080/admin/Login";
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(path);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("username", username));
		parameters.add(new BasicNameValuePair("password", password));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
				"UTF-8");
		httpPost.setEntity(entity);
		HttpResponse response = client.execute(httpPost);

		int code = response.getStatusLine().getStatusCode();
		if (code == 200) {
			InputStream is = response.getEntity().getContent();
			String result = StreamUtils.readStream(is);
			if(result.equals("false")){
				return "用户名或密码错误，请重新输入";
			}else{
			 SharedPrefer.SaveUsername(context, username);
			SharedPrefer.SavePassword(context, password);
			return "student";}
		} else {
			return null;
		}

	}
}
